<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <span>请输入时间戳:</span>
  <input type="text" id="text">
  <button id="sure">确认</button>
  <span>结果:</span>
  <span id="result"></span>
  <script>
    window.onload=function(){
      let sure = document.getElementById("sure")
      sure.onclick=function(){
        let res = document.getElementById("text")
        let newResult = document.getElementById("result")
        if(!isNaN(parseInt(res.value))){
          if(res.value.length >= 10|| res.value.length <= 13){
            newResult.innerHTML = timePrase(Number(res.value))
          }else{
            alert("输入的字符串长度不符!")
          }
        }else{
          alert("输入的数据必须是时间戳格式!")
        }
      }
      function timePrase(num){
        let year = new Date(num).getFullYear()
        let month = new Date(num).getMonth()+1
        let day = new Date(num).getDate()
        let hour = new Date(num).getHours()
        let minutes = new Date(num).getMinutes()
        let sec = new Date(num).getSeconds()
        return year + "-" + addZero(month) + "-" + addZero(day) + " " + addZero(hour) + ":" + addZero(minutes) + ":" + addZero(sec)
      }
      function addZero(num){
        return num >= 10 ? num : "0" + num
      }
    }
  </script>
</body>
</html>